package com.aaron.designmode.prototype;

import java.util.List;

/**
 * @author liurong
 * @date 2021/11/24 10:03
 * @version:
 */
public class PrototypeDemo implements Cloneable {

    String str;

    Integer number;

    MyInteger myInteger;

    @Override
    protected PrototypeDemo clone() {
        try {
            return (PrototypeDemo) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public MyInteger getMyInteger() {
        return myInteger;
    }

    public void setMyInteger(MyInteger myInteger) {
        this.myInteger = myInteger;
    }

    @Override
    public String toString() {
        return "PrototypeDemo{" +
                "str='" + str + '\'' +
                ", number=" + number +
                ", myInteger=" + myInteger +
                '}';
    }

//    public static void main(String[] args) {
//        PrototypeDemo prototypeDemo = new PrototypeDemo();//new完Integer、String都是null
//        prototypeDemo.setNumber(10000);
//        prototypeDemo.setStr("1");
//        MyInteger myInteger = new MyInteger(10000);
//        prototypeDemo.setMyInteger(myInteger);
//        PrototypeDemo prototypeDemoClone = prototypeDemo.clone();
//        myInteger.setNum(99999);
//        prototypeDemoClone.setNumber(20000);
//        prototypeDemoClone.setStr("2");
//        prototypeDemoClone.setMyInteger(new MyInteger(20000));
//        System.out.println(prototypeDemoClone);
//        System.out.println(prototypeDemo);
//    }

    public static void main(String[] args) {
        PrototypeDemo original = new PrototypeDemo();
        original.setMyInteger(new MyInteger(1));
        PrototypeDemo clone = original.clone();

        System.out.println(original);
        System.out.println(clone);

        clone.getMyInteger().setNum(2);
        System.out.println(original);
        System.out.println(clone);
    }

}
//String、Integer等包装类都是不可变的对象，当需要修改不可变对象的值时，需要在内存中生成一个新的对象来存放新的值，然后将原来的引用指向新的地址
/**
 * 原型模式（克隆模式）
 * java clone()
 * 问题：
 * 浅克隆 深克隆
 * 构造函数不会被执行（clone new）
 * clone final
 */

